//
//  LGraffitiPaletteView.m
//
//  Created by 苏沫离 on 2019/1/24.
//  Copyright © 2019 苏沫离. All rights reserved.
//

#import "LGraffitiPaletteView.h"


#define CellIdentifer @"LGraffitiPaletteCell"

/* 10 行 * 12 列
 */
NSMutableArray<UIColor *> *getColorArray(void){
    
    NSMutableArray *colorArray = [NSMutableArray arrayWithCapacity:10 * 12];
    
    [colorArray addObject:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:235/255.0 green:235/255.0 blue:235/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:214/255.0 green:214/255.0 blue:214/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:194/255.0 green:194/255.0 blue:194/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:173/255.0 green:173/255.0 blue:173/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:133/255.0 green:133/255.0 blue:133/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:112/255.0 green:112/255.0 blue:112/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:92/255.0 green:92/255.0 blue:92/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:71/255.0 green:71/255.0 blue:71/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:1.0]];
    
    [colorArray addObject:[UIColor colorWithRed:1/255.0 green:55/255.0 blue:74/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:1/255.0 green:29/255.0 blue:86/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:17/255.0 green:5/255.0 blue:59/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:46/255.0 green:6/255.0 blue:60/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:59/255.0 green:7/255.0 blue:27/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:92/255.0 green:7/255.0 blue:1/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:90/255.0 green:28/255.0 blue:0/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:88/255.0 green:51/255.0 blue:0/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:86/255.0 green:61/255.0 blue:0/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:102/255.0 green:97/255.0 blue:0/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:79/255.0 green:85/255.0 blue:4/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:38/255.0 green:62/255.0 blue:15/255.0 alpha:1.0]];
    
    [colorArray addObject:[UIColor colorWithRed:0/255.0 green:77/255.0 blue:100/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:1/255.0 green:47/255.0 blue:123/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:26/255.0 green:10/255.0 blue:81/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:69/255.0 green:14/255.0 blue:88/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:85/255.0 green:16/255.0 blue:41/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:131/255.0 green:17/255.0 blue:0/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:122/255.0 green:42/255.0 blue:0/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:122/255.0 green:74/255.0 blue:0/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:120/255.0 green:88/255.0 blue:0/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:140/255.0 green:134/255.0 blue:2/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:111/255.0 green:118/255.0 blue:10/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:56/255.0 green:87/255.0 blue:26/255.0 alpha:1.0]];
    
    [colorArray addObject:[UIColor colorWithRed:0/255.0 green:110/255.0 blue:142/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:0/255.0 green:66/255.0 blue:168/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:44/255.0 green:9/255.0 blue:119/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:97/255.0 green:24/255.0 blue:123/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:121/255.0 green:25/255.0 blue:61/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:181/255.0 green:26/255.0 blue:0/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:173/255.0 green:61/255.0 blue:0/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:169/255.0 green:104/255.0 blue:0/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:165/255.0 green:123/255.0 blue:1/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:195/255.0 green:188/255.0 blue:0/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:155/255.0 green:164/255.0 blue:15/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:78/255.0 green:122/255.0 blue:40/255.0 alpha:1.0]];
    
    [colorArray addObject:[UIColor colorWithRed:0/255.0 green:140/255.0 blue:180/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:2/255.0 green:85/255.0 blue:214/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:55/255.0 green:26/255.0 blue:148/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:122/255.0 green:33/255.0 blue:158/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:152/255.0 green:36/255.0 blue:79/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:225/255.0 green:36/255.0 blue:1/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:218/255.0 green:81/255.0 blue:0/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:210/255.0 green:130/255.0 blue:0/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:209/255.0 green:157/255.0 blue:1/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:245/255.0 green:236/255.0 blue:0/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:194/255.0 green:208/255.0 blue:23/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:102/255.0 green:156/255.0 blue:52/255.0 alpha:1.0]];
    
    [colorArray addObject:[UIColor colorWithRed:1/255.0 green:160/255.0 blue:216/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:1/255.0 green:97/255.0 blue:253/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:76/255.0 green:34/255.0 blue:178/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:152/255.0 green:42/255.0 blue:187/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:185/255.0 green:45/255.0 blue:93/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:255/255.0 green:64/255.0 blue:20/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:255/255.0 green:106/255.0 blue:0/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:254/255.0 green:170/255.0 blue:2/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:252/255.0 green:199/255.0 blue:0/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:253/255.0 green:251/255.0 blue:66/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:216/255.0 green:236/255.0 blue:55/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:117/255.0 green:187/255.0 blue:63/255.0 alpha:1.0]];
    
    [colorArray addObject:[UIColor colorWithRed:3/255.0 green:199/255.0 blue:251/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:58/255.0 green:135/255.0 blue:253/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:94/255.0 green:48/255.0 blue:235/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:190/255.0 green:55/255.0 blue:242/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:230/255.0 green:59/255.0 blue:122/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:254/255.0 green:98/255.0 blue:79/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:255/255.0 green:134/255.0 blue:72/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:254/255.0 green:180/255.0 blue:63/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:254/255.0 green:203/255.0 blue:62/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:254/255.0 green:247/255.0 blue:106/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:228/255.0 green:238/255.0 blue:101/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:150/255.0 green:211/255.0 blue:95/255.0 alpha:1.0]];
    
    [colorArray addObject:[UIColor colorWithRed:82/255.0 green:214/255.0 blue:251/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:116/255.0 green:166/255.0 blue:255/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:134/255.0 green:78/255.0 blue:253/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:211/255.0 green:87/255.0 blue:253/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:238/255.0 green:113/255.0 blue:158/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:254/255.0 green:140/255.0 blue:130/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:255/255.0 green:165/255.0 blue:125/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:255/255.0 green:199/255.0 blue:119/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:254/255.0 green:217/255.0 blue:119/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:255/255.0 green:249/255.0 blue:147/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:234/255.0 green:241/255.0 blue:143/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:177/255.0 green:220/255.0 blue:138/255.0 alpha:1.0]];
    
    [colorArray addObject:[UIColor colorWithRed:147/255.0 green:226/255.0 blue:252/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:167/255.0 green:198/255.0 blue:255/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:177/255.0 green:140/255.0 blue:253/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:225/255.0 green:145/255.0 blue:253/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:244/255.0 green:163/255.0 blue:192/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:255/255.0 green:181/255.0 blue:175/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:255/255.0 green:197/255.0 blue:171/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:254/255.0 green:217/255.0 blue:168/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:253/255.0 green:227/255.0 blue:168/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:255/255.0 green:251/255.0 blue:185/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:241/255.0 green:246/255.0 blue:183/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:205/255.0 green:232/255.0 blue:180/255.0 alpha:1.0]];
    
    [colorArray addObject:[UIColor colorWithRed:203/255.0 green:239/255.0 blue:255/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:210/255.0 green:225/255.0 blue:255/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:217/255.0 green:200/255.0 blue:254/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:239/255.0 green:202/255.0 blue:255/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:248/255.0 green:211/255.0 blue:223/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:255/255.0 green:219/255.0 blue:216/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:255/255.0 green:226/255.0 blue:213/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:254/255.0 green:235/255.0 blue:211/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:254/255.0 green:242/255.0 blue:213/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:253/255.0 green:252/255.0 blue:221/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:247/255.0 green:249/255.0 blue:219/255.0 alpha:1.0]];
    [colorArray addObject:[UIColor colorWithRed:222/255.0 green:237/255.0 blue:212/255.0 alpha:1.0]];
    
    return colorArray;
}

@interface LGraffitiPaletteCell : UICollectionViewCell
@end
@implementation LGraffitiPaletteCell

- (void)setSelected:(BOOL)selected{
    [super setSelected:selected];
    

    if (selected) {
        self.layer.borderWidth = 2.0;
        self.layer.borderColor = UIColor.whiteColor.CGColor;
//        CGFloat red = 0;
//        CGFloat green = 0;
//        CGFloat blue = 0;
//        [self.contentView.backgroundColor getRed:&red green:&green blue:&blue alpha:NULL];
//        red = (1 - red) < 0.2 ? 0.0 : 1.0;
//        green = (1 - green) < 0.2 ? 0.0 : 1.0;
//        blue = (1 - blue) < 0.2 ? 0.0 : 1.0;
//        self.layer.borderColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0].CGColor;
    }else{
        self.layer.borderWidth = 0.0;
        self.layer.borderColor = UIColor.clearColor.CGColor;
    }
}

@end


@interface LGraffitiPaletteView ()
<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic ,strong) UIButton *coverButton;
@property (nonatomic ,strong) UICollectionView *collectionView;
@property (nonatomic ,strong) NSMutableArray<UIColor *> *colorArray;

@end

@implementation LGraffitiPaletteView

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.frame = UIScreen.mainScreen.bounds;
        [self addSubview:self.coverButton];
        [self addSubview:self.collectionView];
        
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            self.colorArray = getColorArray();
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.collectionView reloadData];
            });
        });
        
    }
    return self;
}


#pragma mark - public method

- (void)show
{
    __weak typeof(self) weakSelf = self;
    self.collectionView.transform = CGAffineTransformMakeTranslation(0, CGRectGetHeight(self.collectionView.frame));
    [UIApplication.sharedApplication.delegate.window addSubview:self];
    self.coverButton.alpha = 0;
    [UIView animateWithDuration:0.2 animations:^{
        self.coverButton.alpha = 1.0;
        weakSelf.collectionView.transform = CGAffineTransformMakeTranslation(0, 0);
    } completion:^(BOOL finished) {}];
}

- (void)dismissButtonClick
{
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:0.2 animations:^{
        weakSelf.coverButton.alpha = 0;
        weakSelf.collectionView.transform = CGAffineTransformMakeTranslation(0, CGRectGetHeight(weakSelf.collectionView.frame));
    } completion:^(BOOL finished) {
        [weakSelf.coverButton removeFromSuperview];
        weakSelf.coverButton = nil;
        [weakSelf.collectionView removeFromSuperview];
        weakSelf.collectionView = nil;
        [weakSelf removeFromSuperview];
    }];
}

#pragma mark - UICollectionViewDelegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.colorArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    LGraffitiPaletteCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifer forIndexPath:indexPath];
    cell.contentView.backgroundColor = self.colorArray[indexPath.row];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

}

#pragma mark - setter and getter

- (UIButton *)coverButton{
    if (_coverButton == nil) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.2];
        button.frame = UIScreen.mainScreen.bounds;
        [button addTarget:self action:@selector(dismissButtonClick) forControlEvents:UIControlEventTouchUpInside];
        _coverButton = button;
    }
    return _coverButton;
}

- (UICollectionView *)collectionView{
    if (_collectionView == nil) {
        
        CGFloat left = 16.0;
        CGFloat width = CGRectGetWidth(UIScreen.mainScreen.bounds) - left * 2;
        UIEdgeInsets edgeInsets = UIEdgeInsetsMake(4, 4, 4, 4);
        CGFloat itemWidth = (width - edgeInsets.left - edgeInsets.right) / 12.0;
        CGFloat height = itemWidth * 10.0 + edgeInsets.top + edgeInsets.bottom;
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake(itemWidth, itemWidth);
        layout.minimumInteritemSpacing = 0.0f;
        layout.minimumLineSpacing = 0.0f;
        layout.sectionInset = edgeInsets;
        
        UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(16,100,width ,height) collectionViewLayout:layout];
        collectionView.backgroundColor = [UIColor whiteColor];
        collectionView.delegate = self;
        collectionView.dataSource = self;
        collectionView.pagingEnabled = YES;
        collectionView.bounces = NO;
        collectionView.showsHorizontalScrollIndicator = NO;
        collectionView.showsVerticalScrollIndicator = NO;
        [collectionView registerClass:LGraffitiPaletteCell.class forCellWithReuseIdentifier:CellIdentifer];
        collectionView.contentOffset = CGPointMake(0, 0);
        
        _collectionView = collectionView;
    }
    return _collectionView;
}



@end
